package com.aco.party.web.controller.system;

import com.aco.party.enums.MetaTypeEnum;
import com.aco.party.model.po.Company;
import com.aco.party.model.vo.CompanyParam;
import com.aco.party.model.vo.CompanyVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.CompanyService;
import com.aco.party.service.system.MetaService;
import com.aco.party.web.command.system.EditCompanyCommand;
import com.aco.party.web.validator.system.EditCompanyValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.aco.party.util.WebModelUtil.*;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManageCompanyController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ManageCompanyController.class);

    @Resource
    private CompanyService companyService;

    @Resource
    private MetaService metaService;

    private EditCompanyValidator validator = new EditCompanyValidator();

    @RequestMapping(value = {"/company", "/company/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manageCompanys";
    }

    @RequestMapping(value = "company/list", method = RequestMethod.POST)
    @ResponseBody
    public Page <CompanyVo> getListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <CompanyVo> page = new Page<>();
        entry.webParamConvert();
        List <CompanyVo> list = companyService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),new CompanyParam());
        page.setTotal(companyService.getTotal(new CompanyParam()));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/company/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response, @ModelAttribute EditCompanyCommand command, Model model) {
        if (command.getId() != null) {
            Company po = companyService.getByPrimaryKey(command.getId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
        }
        setAtt(model);
        return "system/editCompany";
    }

    @RequestMapping(value = "/company/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute EditCompanyCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Company company = new Company();
            command.toPo(company);
            company.setUpdator(getCurrentUser().getId());
            company.setUpdateTime(new Date());
            companyService.update(company);
            model.addAttribute("no_error", true);
        }

        return "system/editCompany";
    }

    @RequestMapping(value = "/company/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditCompanyCommand command,Model model) {
        command.setStatus(1);
        command.setType(1);
        setAtt(model);
        return "system/editCompany";
    }

    @RequestMapping(value = "/company/add", method = RequestMethod.POST)
    public String add(Model model, @ModelAttribute EditCompanyCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editCompany";
        }
        Company company = new Company();
        command.toPo(company);
        company.setCreator(getCurrentUser().getId());
        company.setCreateTime(new Date());
        company.setUpdateTime(new Date());
        companyService.add(company);
        attributes.addAttribute("id",company.getId());
        attributes.addFlashAttribute("no_error", true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/company/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id) {
        logger.info("删除：" + id);
        companyService.delete(id);
        return new Response();
    }

    private void setAtt(Model model){
        model.addAttribute("types", convert2Pairs(metaService.getByType(MetaTypeEnum.公司类型.getValue())));
    }

}
